// Copyright 2013 Petar Maymounkov
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package fuserh

import (
	"sync"

	"circuit/kit/fuse"
	"circuit/kit/rh"
)

// NodeTable
type NodeTable struct {
	sync.Mutex
	id map[fuse.NodeID]rh.FID // Slice index is fuse.NodeID
	n  fuse.NodeID

	// FUSE requires nodeids to have associated generation numbers.
	// If we reuse a nodeid, we have to bump the generation number to
	// guarantee that the nodeid,gen combination is never reused.
	gen uint64 // FUSE generation
}

func (n *NodeTable) Init() {
	n.id = make(map[fuse.NodeID]rh.FID)
	n.gen = 1
}

func (n *NodeTable) Size() int {
	n.Lock()
	defer n.Unlock()
	return len(n.id)
}

func (n *NodeTable) Lookup(nid fuse.NodeID) rh.FID {
	n.Lock()
	defer n.Unlock()
	return n.id[nid]
}

func (n *NodeTable) Add(fid rh.FID) (nid fuse.NodeID, gen uint64) {
	n.Lock()
	defer n.Unlock()
	n.n++ // zero is not a valid nid
	n.id[n.n] = fid
	return n.n, n.gen // We don't increment the generation, because node IDs are never reused.
}

func (n *NodeTable) Scrub(nid fuse.NodeID) rh.FID {
	n.Lock()
	defer n.Unlock()
	fid, ok := n.id[nid]
	if !ok {
		return nil
	}
	delete(n.id, nid)
	return fid
}
